#!/usr/bin/env groovy

@Library('shared-pipelines') _
import org.mattermost.Utils
import java.util.*;
def utils = new org.mattermost.Utils()
def build = new org.mattermost.BuildCommands()
def platformStages = new org.mattermost.PlatformStages()

def rndEE = UUID.randomUUID().toString()
def rndTE = UUID.randomUUID().toString()
def mmBuilderServer = 'mattermost/mattermost-build-server:20200731_golang-1.14.6'
def mmBuilderWebapp = 'mattermost/mattermost-build-webapp:oct-2-2018'

pipeline {
  agent {
    label 'default-mm-builder'
  }

  options {
    buildDiscarder(logRotator(daysToKeepStr: '15'))
    timeout(time: 2, unit: 'HOURS')
  }

  environment {
    COMPOSE_PROJECT_NAME="${rnd}-${env.BUILD_NUMBER}"
    GOPATH = "/go"
  }

  stages {
    stage('Setup') {
      steps {
        script {
          platformStages.platformEcho()
        }
      }
    }

    stage('Build') {
      failFast true
      parallel {
        stage ('Build EE') {
          agent {
            label 'default-mm-builder'
          }
          steps {
            script {
              forcefullyCleanWorkspace(mmBuilderServer)
              platformStages.platformCheckoutTE(env.BRANCH_NAME)
              platformStages.platformCheckoutEE(env.BRANCH_NAME)
              platformStages.platformCheckoutRedux(env.BRANCH_NAME)
              SERVER_GIT_COMMIT=platformStages.platformServerCommitHash()
              try {
                platformStages.after5Dot20AddGOBINBuild(env.BRANCH_NAME + '.' + env.BUILD_NUMBER + '.' + SERVER_GIT_COMMIT, rndEE, false, mmBuilderServer, mmBuilderWebapp)
              } finally {
                platformStages.platformPublishResults()
                platformStages.platformCleanDockerCompose(rndEE)
              }
            }
            stash includes: 'src/github.com/mattermost/mattermost-server/dist/**', name: 'eedist'
          }
        }
        stage ('Build TE') {
          agent {
            label 'default-mm-builder'
          }
          steps {
            script {
              forcefullyCleanWorkspace(mmBuilderServer)
              platformStages.platformCheckoutTE(env.BRANCH_NAME)
              platformStages.platformCheckoutRedux(env.BRANCH_NAME)
              SERVER_GIT_COMMIT=platformStages.platformServerCommitHash()
              try {
                platformStages.after5Dot20AddGOBINBuild(env.BRANCH_NAME + '.' + env.BUILD_NUMBER + '.' + SERVER_GIT_COMMIT, rndTE, false, mmBuilderServer, mmBuilderWebapp)
              } finally {
                platformStages.platformPublishResults()
                platformStages.platformCleanDockerCompose(rndTE)
              }
            }
            stash includes: 'src/github.com/mattermost/mattermost-server/dist/**', name: 'tedist'
          }
        }
      }
    }

    stage ('Push EE') {
      steps {
        node('master') {
          forcefullyCleanWorkspace(mmBuilderServer)
          unstash 'eedist'
          script {
            platformStages.pushToS3('mattermost-platform/' + env.BRANCH_NAME, 'mattermost-enterprise-*',false)
          }
        }
      }
    }
    stage ('Push TE') {
      steps {
        node('master') {
          forcefullyCleanWorkspace(mmBuilderServer)
          unstash 'tedist'
          script {
            platformStages.pushToS3('mattermost-platform/' + env.BRANCH_NAME, 'mattermost-team-*',false)
          }
        }
      }
    }
    stage ('CleanCloudfront') {
      steps {
        node('master') {
          script {
            platformStages.cleanCloudfrontCache('mattermost-platform/' + env.BRANCH_NAME)
          }
        }
      }
    }

    stage ('Build Docker Images') {
      agent {
        label 'default-mm-builder'
      }
      steps {
        script {
          forcefullyCleanWorkspace(mmBuilderServer)
          platformStages.platformCheckoutTE(env.BRANCH_NAME)
        }
        dir('src/github.com/mattermost/mattermost-server') {
          withCredentials([usernamePassword(credentialsId: 'matterbuild-docker-hub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
            sh 'docker login --username ${DOCKER_USER} --password ${DOCKER_PASS}'
            sh """
            export TAG='${env.BRANCH_NAME}'
            docker build --no-cache --build-arg MM_PACKAGE=https://releases.mattermost.com/mattermost-platform/${env.BRANCH_NAME}/mattermost-enterprise-linux-amd64.tar.gz -t mattermost/mattermost-enterprise-edition:\${TAG} build
            docker push mattermost/mattermost-enterprise-edition:\${TAG}
            """
            sh """
            export TAG='${env.BRANCH_NAME}'
            docker build --no-cache --build-arg MM_PACKAGE=https://releases.mattermost.com/mattermost-platform/${env.BRANCH_NAME}/mattermost-team-linux-amd64.tar.gz -t mattermost/mattermost-team-edition:\${TAG} build
            docker push mattermost/mattermost-team-edition:\${TAG}
            docker logout
            """
          }
        }
      }
    }
  }

  post {
    cleanup {
      forcefullyCleanWorkspace(mmBuilderServer)
    }
  }
}


def forcefullyCleanWorkspace(String imageToUse) {
  return withDockerContainer(
    image: imageToUse,
    args: '-u root'
  ) {
    ansiColor('xterm') {
      sh """#!/bin/bash -e
        if [ -d "\$WORKSPACE" ]
        then
          rm -rfv \$WORKSPACE/*
        fi
      """
    }
  }
}
